package com.skyline.base.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.skyline.base.model.Notification;
import com.skyline.base.service.NoticeService;
import com.skyline.base.type.NotificationType;
import com.skyline.common.util.Constant;
import com.skyline.common.util.WebHelper;
import com.skyline.user.model.User;

/**
 * 通知的Controller
 * 
 * @author jairus
 * 
 * @version 0.1
 */
@Controller
@RequestMapping("/notice")
public class NoticeController extends BaseController {

	@Autowired
	private NoticeService noticeService;

	/**
	 * getNotification,获取某个人自己所收到的通知,提供ajax调用
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @return 所查询到的通知列表
	 * 
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody
	List<Notification> getNotification() {

		User user = (User) WebHelper.getSessionAttribute(null, Constant.SESSION_USER);
		if (user == null) {
			return null;
		} else {
			List<Notification> notifications = noticeService.getNotificationByOwnerId(user.getId());
			return notifications;
		}
	}

	/**
	 * viewNotifiction，浏览通知：系统将自动跳转到相应的页面
	 * 
	 * @param id
	 *            通知的ID
	 * @return ModelAndView
	 */
	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public ModelAndView viewNotifiction(@PathVariable Long id) {
		Notification notification = noticeService.getNotificationById(id);
		String viewName = null;
		if (notification != null) {
			if (notification.getType().equals(NotificationType.ARTICLE_COMMENT)) {
				viewName = "/article/view/" + notification.getResourceId();
			} else if (notification.getType().equals(NotificationType.ALBUM_COMMENT)) {
				// TODO:
			} else if (notification.getType().equals(NotificationType.FOLLOW)) {
				// TODO:
			} else if (notification.getType().equals(NotificationType.MANAGE_NOTIFICATION)) {
				// TODO:
			} else if (notification.getType().equals(NotificationType.PHOTO_COMMENT)) {
				// TODO:
			}else if(notification.getType().equals(NotificationType.SHORTMESSAGE_COMMENT)){
				viewName = "/shortMessage/view/" + notification.getResourceId();
			}else if(notification.getType().equals(NotificationType.INVITATION) && notification.getResourceTitle().equals("景点邀请")){
				viewName = "/attraction/invite/view/receive/" + notification.getResourceId();
			}
		}
//		viewName += URL_SUFFIX;
		ModelAndView mav = new ModelAndView();
		String url = buildRecirectPath(viewName);
		mav.setView(new RedirectView(url));
		return mav;
	}
}
